package org.szy.plugin.dev.controller;

import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.ModelAndView;
import org.noear.solon.validation.annotation.NoRepeatSubmit;
import org.noear.solon.validation.annotation.NotNull;
import org.noear.solon.validation.annotation.Validated;
import org.szy.core.common.controller.BaseController;
import org.szy.core.common.entity.R;

import org.szy.core.common.param.CommonMoveParam;
import org.szy.core.common.param.CommonStatusParam;
import org.szy.plugin.dev.entity.DevArea;
import org.szy.plugin.dev.param.DevAreaQueryParam;
import org.szy.plugin.dev.pojo.DevAreaTree;
import org.szy.plugin.dev.pojo.DevAreaTreeOption;
import org.szy.plugin.dev.service.DevAreaService;

import java.util.List;

/**
 * 中国行政地区表控制器
 *
 * @author wangbuhao
 * @since 2023-06-19 11:08:21
 */
@Controller
@Mapping("/dev/area")
public class DevAreaController extends BaseController<DevArea> {

    private final String PATH_PREFIX = "dev/area/";
    @Inject
    private DevAreaService devAreaService;

    /**
     * 中国行政地区表列表页面
     */
    @Get
    @Mapping("/index")
    public ModelAndView index() {
        ModelAndView view = new ModelAndView(PATH_PREFIX + "index");
        return view;
    }

    /**
     * 中国行政地区表编辑页
     */
    @Get
    @Mapping("/edit")
    public ModelAndView edit() {
        ModelAndView view = new ModelAndView(PATH_PREFIX + "edit");
        return view;
    }

    /**
     * 中国行政地区表详情页
     */
    @Get
    @Mapping("/detail")
    public ModelAndView detail() {
        ModelAndView view = new ModelAndView(PATH_PREFIX + "detail");
        return view;
    }


    /**
     * 获取行政区域树形数据
     *
     * @author wangbuhao
     * @since 2023-06-18 17:12:42
     */
    @Get
    @Mapping("/treeList")
    public R<List<DevAreaTree>> treeList(DevAreaQueryParam param) {
        List<DevArea> list = devAreaService.list(param);
        List<DevAreaTree> trees = devAreaService.buildTrees(list);
        return R.data(trees);
    }

    /**
     * 获取行政区域数据结构选项
     * @param param 查询参数
     * @return
     */
    @Get
    @Mapping("/treeOption")
    public R<List<DevAreaTreeOption>> treeOption(DevAreaQueryParam param) {
        List<DevArea> list = devAreaService.list(param);
        List<DevAreaTree> trees = devAreaService.buildTrees(list);
        List<DevAreaTreeOption> treeOption = DevAreaTreeOption.buildTreeOption(trees);
        return R.data(treeOption);
    }

    /**
     * 获取中国行政地区表详情
     *
     * @author wangbuhao
     * @since 2023-06-19 11:08:21
     */
    @Get
    @Mapping("/getInfo")
    public R<DevArea> getInfo(@NotNull Long id) {
        return R.asNull(devAreaService.getById(id));
    }

    /**
     * 添加中国行政地区表
     *
     * @author wangbuhao
     * @since 2023-06-19 11:08:21
     */
    @Post
    @Mapping("/add")
    @NoRepeatSubmit
    public R<?> add(@Validated DevArea entity) {
        boolean save = devAreaService.add(entity);
        return save ? R.ok("保存成功", entity.getId()) : R.fail("保存失败");
    }

    /**
     * 编辑中国行政地区表
     *
     * @author wangbuhao
     * @since 2023-06-19 11:08:21
     */
    @Put
    @Mapping("/update")
    @NoRepeatSubmit
    public R<?> update(@Validated DevArea entity) {
        boolean update = devAreaService.edit(entity);
        return R.bool(update);
    }

    /**
     * 删除中国行政地区表
     *
     * @author wangbuhao
     * @since 2023-06-19 11:08:21
     */
    @Delete
    @Mapping("/delete")
    public R<?> delete(@NotNull Long id) {
        boolean delete = devAreaService.deleteById(id);
        return R.bool(delete, "删除成功", "删除失败，数据可能正在使用或已删除");
    }

    /**
     * 修改状态
     *
     * @author wangbuhao
     * @date 2023年2月13日21:02:00
     */
    @Put
    @Mapping("/modifyStatus")
    public R<?> modifyStatus(@Validated CommonStatusParam statusParam) {
        boolean status = devAreaService.modifyStatus(statusParam);
        return R.bool(status);
    }

    /**
     * 移动区域
     *
     * @author wangbuhao
     * @date 2023年2月15日10:25:15
     */
    @Put
    @Mapping("/move")
    public R<?> move(CommonMoveParam commonMoveParam) {
        boolean move = devAreaService.move(commonMoveParam);
        return R.bool(move);
    }

}
